package com.leng.util;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;


/**
 * 百度编辑器辅助类
 * 
 * @author Administrator
 *
 */
public class UEditorUtils {
	private static Logger logger = Logger.getLogger(UEditorUtils.class);
	private final static String QINIU_HOST="http://singerimg.cavca.net";//七牛服务器

	/**
	 * 将编辑内容中的本地url链接图片代表的本地图片上传到服务器并替换原来的链接
	 * 
	 * @param source 富文本内容
	 * @param request
	 * @return
	 */

	public static String upload(String source, HttpServletRequest request) {
		List<String> imgs = HtmlUtil.matchAll(source, "img", "src");
		logger.error("上传文本中的图片...待上传图片集合imgs=" + (imgs == null ? null : Arrays.toString(imgs.toArray())));
		if (imgs != null && !imgs.isEmpty()) {
			String addr = request.getServerName();// 域名
			String projectName = request.getContextPath();// 项目名
			File baseFile = new File(request.getSession().getServletContext().getRealPath("/"));
			logger.error("上传文本中的图片...addr=" + addr + ",projectName=" + projectName + ",baseFilePath="
					+ baseFile.getAbsolutePath());

			for (String img : imgs) {
				if (!img.contains(addr)) {// 只上传本地文件
					continue;
				}
				String relativePath = MyUrlUtil.getURI(img, projectName != null && projectName.trim().length() >0);// 相对路径
				logger.error("上传文本中的图片...relativePath=" + relativePath);
				if (relativePath == null) {
					continue;
				}
				File file = new File(baseFile, relativePath);// 本地文件
				logger.error("上传文本中的图片...本地图片路径=" + file.getAbsolutePath());
				// 上传本地文件到七牛服务器
				//String imgUrl = UrlUtils.upload(file, file.getName(), "false", 0);
				//source = StringUtils.replace(source, img, imgUrl, 1);
			}
		}
		return source;
	}

	/**
	 * 修改富文本内容
	 * 
	 * @param oldStr
	 *            保存在数据库中的内容
	 * @param newStr
	 *            新提交来的内容
	 * @param request
	 * @return
	 */
	public static String modify(String oldStr, String newStr, HttpServletRequest request) {

		newStr = upload(newStr, request);

		List<String> oldImgs = HtmlUtil.matchAll(oldStr, "img", "src");// 旧图片url集合
		if (oldImgs != null && !oldImgs.isEmpty()) {
			List<String> deleteFiles = new ArrayList<String>();
			for (String img : oldImgs) {
				if (isServerImg(img)) {// 是七牛的图片
					if (newStr.contains(img)) {
						continue;
					}
					deleteFiles.add(img);
				}
			}
			logger.error("修改富文本内容...待删除的图片集合="+Arrays.toString(deleteFiles.toArray()));
			//UrlUtils.deleteFiles(deleteFiles);
		}
		return newStr;
	}

	/**
	 * 删除服务器上的图片
	 * 
	 * @param source 富文本内容
	 */
	public static void delete(String source) {
		List<String> oldImgs = HtmlUtil.matchAll(source, "img", "src");// 旧图片url集合
		if (oldImgs != null && !oldImgs.isEmpty()) {
			List<String> deleteFiles = new ArrayList<String>();
			for (String img : oldImgs) {
				if (isServerImg(img)) {// 是七牛的图片
					deleteFiles.add(img);
				}
			}
			logger.error("删除服务器上的图片...待删除的图片集合="+Arrays.toString(deleteFiles.toArray()));
			//UrlUtils.deleteFiles(deleteFiles);
		}
	}

	private static boolean isServerImg(String imgUrl) {
		return imgUrl.contains(QINIU_HOST);
	}

}
